package com.duolaibei.business.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.duolaibei.business.R;


/**
 * Created by xlm on 2017/8/9 0009
 */

public class DlKeyboard extends RelativeLayout implements View.OnClickListener {

    private Animation animationIn;
    private Animation animationOut;
    private TextView mText0;
    private TextView mText1;
    private TextView mText2;
    private TextView mText3;
    private TextView mText4;
    private TextView mText5;
    private TextView mText6;
    private TextView mText7;
    private TextView mText8;
    private TextView mText9;
    private TextView mTextDian;
    private TextView mTextConfirm;
    private TextView mTextCode;
    private RelativeLayout mImageBack;
    private RelativeLayout mImageDelete;

    public DlKeyboard(Context context) {
        this(context, null);
    }

    public DlKeyboard(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DlKeyboard(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);
    }

    private void init(Context context, AttributeSet attrs, int defStyleAttr) {
        View view = LayoutInflater.from(context).inflate(R.layout.dl_keyboard, this);
        mText0 = (TextView) view.findViewById(R.id.key0);
        mText1 = (TextView) view.findViewById(R.id.key1);
        mText2 = (TextView) view.findViewById(R.id.key2);
        mText3 = (TextView) view.findViewById(R.id.key3);
        mText4 = (TextView) view.findViewById(R.id.key4);
        mText5 = (TextView) view.findViewById(R.id.key5);
        mText6 = (TextView) view.findViewById(R.id.key6);
        mText7 = (TextView) view.findViewById(R.id.key7);
        mText8 = (TextView) view.findViewById(R.id.key8);
        mText9 = (TextView) view.findViewById(R.id.key9);
        mTextCode = (TextView) view.findViewById(R.id.qrcode);
        mTextDian = (TextView) view.findViewById(R.id.key_dian);
        mTextConfirm = (TextView) view.findViewById(R.id.key_confirm);
        mImageBack = (RelativeLayout) view.findViewById(R.id.key_back);
        mImageDelete = (RelativeLayout) view.findViewById(R.id.key_delete);
        initListener();
        initAnimation();
    }

    private void initListener() {
        mText0.setOnClickListener(this);
        mText1.setOnClickListener(this);
        mText2.setOnClickListener(this);
        mText3.setOnClickListener(this);
        mText4.setOnClickListener(this);
        mText5.setOnClickListener(this);
        mText6.setOnClickListener(this);
        mText7.setOnClickListener(this);
        mText8.setOnClickListener(this);
        mText9.setOnClickListener(this);
        mTextCode.setOnClickListener(this);
        mTextDian.setOnClickListener(this);
        mTextConfirm.setOnClickListener(this);
        mImageBack.setOnClickListener(this);
        mImageDelete.setOnClickListener(this);
    }

    // 初始化动画效果
    private void initAnimation() {
        animationIn = AnimationUtils.loadAnimation(getContext(), R.anim.keyboard_in);
        animationOut = AnimationUtils.loadAnimation(getContext(), R.anim.keyboard_out);
    }

    // 弹出软键盘
    public void show() {
        startAnimation(animationIn);
        setVisibility(VISIBLE);
    }

    // 关闭软键盘
    public void dismiss() {
        if (isVisible()) {
            startAnimation(animationOut);
            setVisibility(GONE);
        }
    }

    // 判断软键盘的状态
    public boolean isVisible() {
        if (getVisibility() == VISIBLE) {
            return true;
        }
        return false;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.key0:
                listener.clickTxt("0");
                break;
            case R.id.key1:
                listener.clickTxt("1");
                break;
            case R.id.key2:
                listener.clickTxt("2");
                break;
            case R.id.key3:
                listener.clickTxt("3");
                break;
            case R.id.key4:
                listener.clickTxt("4");
                break;
            case R.id.key5:
                listener.clickTxt("5");
                break;
            case R.id.key6:
                listener.clickTxt("6");
                break;
            case R.id.key7:
                listener.clickTxt("7");
                break;
            case R.id.key8:
                listener.clickTxt("8");
                break;
            case R.id.key9:
                listener.clickTxt("9");
                break;
            case R.id.qrcode:
                listener.clickQrcode();
                break;
            case R.id.key_dian:
                listener.clickTxt(".");
                break;
            case R.id.key_confirm:
                listener.clickConfirm();
                break;
            case R.id.key_back:
                listener.clickBack();
                break;
            case R.id.key_delete:
                listener.clickDelete();
                break;
        }
    }

    public void setDlKeyboardListener(OnKeyboardListener dlKeyboardListener) {
        this.listener = dlKeyboardListener;
    }

    private OnKeyboardListener listener;


    public interface OnKeyboardListener {
        void clickTxt(String txt);

        void clickConfirm();

        void clickBack();

        void clickDelete();

        void clickQrcode();
    }

}
